/*
 * @Author: Setsuna
 * @Email: 601890383@qq.com
 * @FilePath: \src\pages\QuotaManage\ManualInventoryRecord\components\EditCheckStockDetail.tsx
 * @Date: 2021-11-26 09:31:34
 * @LastEditTime: 2022-05-31 14:41:36
 */
import React, { createRef } from 'react'
import { http, IUdFormItem, UdForm, validators } from '@/lib'
import { Input, Select, InputNumber, message } from 'antd'
import { WrappedFormUtils } from 'antd/lib/form/Form'
import apis from '@/apis/AboutQuota'
import './approve.scss'

const { TextArea } = Input
interface IProps {
  record: any
  handQuery?: any
}
interface IState {
  record: any
  nowMove: null | number
}
export default class EditCheckStockDetail extends React.Component<IProps, IState> {
  private record = { ...this.props.record }
  labelCol: any = { span: 4 }
  private wrapperCol: any = { span: 20 }
  private form: WrappedFormUtils
  public cRef = createRef<any>()

  state = {
    record: {},
    nowMove: null,
  }

  componentDidMount() { 
    this.setState({nowMove:this.record.nowMove})
  }

  private onOk = e => {
    let { record } = this.props
    this.form.validateFields((err, values) => {
      if (!err) {
        e.drawerBtnLoading()
        http
          .post(apis.editCheckStockDetail, { ...record, ...values })
          .then(res => {
            message.success('操作成功')
            e.close()
          })
          .catch(() => {
            e.drawerBtnLoadingHide()
          })
      } else {
        console.log(err)
      }
    })
  }

  //计算期间动销数
  private calcNowMove = () => {
    setTimeout(() => {
      let formValues = this.form.getFieldsValue()
      let beginningRealStock = formValues['beginningRealStock'] ?? null //期初库存
      let nowAog = formValues['nowAog'] ?? null //期间开单
      let allocateNumber = formValues['allocateNumber'] ?? null //调入调出
      let nowRealStock = formValues['nowRealStock'] ?? null //当前实物库存
      let onOrder = formValues['onOrder'] ?? null //当前未到货
      let nowMove = +beginningRealStock + +nowAog + +allocateNumber - +nowRealStock - +onOrder
      this.setState({ nowMove })
      this.form.setFieldsValue({ nowMove })
    }, 100)
  }

  render() {
    let { nowMove } = this.state
    let items: IUdFormItem[] = [
      { label: '销售组织', id: 'brandNameShort', render: () => <Input readOnly /> },
      { label: '客户名称', id: 'dealerName', render: () => <Input readOnly /> },
      { label: '盘点周期', id: 'description', render: () => <Input readOnly /> },
      { label: '物料描述', id: 'matName', render: () => <Input readOnly /> },
      {
        label: '期初库存(含未到货)件',
        id: 'beginningRealStock',
        rules: [validators.required],
        render: () => <InputNumber onBlur={this.calcNowMove} precision={1} style={{ width: '100%' }} />,
      },
      {
        label: '期间开单/件',
        id: 'nowAog',
        rules: [validators.required],
        render: () => <InputNumber onBlur={this.calcNowMove} precision={1} style={{ width: '100%' }} />,
      },
      {
        label: '调入调出',
        id: 'allocateNumber',
        rules: [validators.required],
        render: () => <InputNumber onBlur={this.calcNowMove} precision={1} style={{ width: '100%' }} />,
      },
      {
        label: '当前实物库存/件',
        id: 'nowRealStock',
        rules: [validators.required],
        render: () => <InputNumber min={0} onBlur={this.calcNowMove} precision={1} style={{ width: '100%' }} />,
      },
      {
        label: '当前未到货/件',
        id: 'onOrder',
        rules: [validators.required],
        render: () => <InputNumber min={0} style={{ width: '100%' }} precision={1} onBlur={this.calcNowMove} />,
      },
      { label: '期间动销数/件', id: 'nowMove', render: () => <InputNumber readOnly style={{ width: '100%' }} /> },
      {
        label: '下月订单计划/件',
        id: 'nextMonthOrderNum',
        render: () => <InputNumber precision={1} style={{ width: '100%' }} />,
      },
      {
        label: '动销说明',
        id: 'nowMoveDes',
        rules: [nowMove !== null ? (nowMove < 0 ? validators.required : { required: false }) : { required: false }],
        render: <TextArea rows={3} />
      },
      { label: '备注信息', id: 'remark', render: <TextArea rows={3} /> },
    ]
    return (
      <>
        <UdForm
          labelCol={this.labelCol}
          wrapperCol={this.wrapperCol}
          getForm={form => (this.form = form)}
          items={items}
          values={this.record}
        />
      </>
    )
  }
}
